// 导出创建切片的方法
import { createSlice } from "@reduxjs/toolkit";


// 创建一个切片
const appSlice = createSlice({
    name: 'app', // 切片名称
    initialState: {
        // 初始数据
        collapsed: localStorage.getItem('collapsed') === 'true',
        // 默认侧边栏打开的菜单
        defaultOpenKeys:localStorage.getItem('defaultOpenKeys') || '/',
          // 默认打开的侧边栏
        defaultSelectedKeys:localStorage.getItem('defaultSelectedKeys') || 'home', 
    },
    reducers: {
        // 修改方法
        changeCollapsed(state, actions) {
            state.collapsed = actions.payload

            // 将数据保存在本地，防止刷新页面的时候数据丢失
            localStorage.setItem('collapsed', actions.payload)
        },
        changePath:(state, actions) => {
            state.defaultOpenKeys = actions.payload.defaultOpenKeys
            state.defaultSelectedKeys = actions.payload.defaultSelectedKeys

            // 将状态保存起来
            localStorage.setItem('defaultOpenKeys', actions.payload.defaultOpenKeys)
            localStorage.setItem('defaultSelectedKeys', actions.payload.defaultSelectedKeys)
        }
    }
})

export const { changeCollapsed ,changePath} = appSlice.actions
export default appSlice.reducer
